package org.lanqiao.ssm.web.auth.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.lanqiao.ssm.common.facede.BaseManagerFacedeI;
import org.lanqiao.ssm.common.facede.BaseQueryFacedeI;
import org.lanqiao.ssm.common.model.EasyuiModel;
import org.lanqiao.ssm.common.model.Json;
import org.lanqiao.ssm.common.util.ModelConvertUtil;
import org.lanqiao.ssm.common.web.controller.BaseController;
import org.lanqiao.ssm.facede.auth.facede.UserManagerFacedeI;
import org.lanqiao.ssm.facede.auth.facede.UserQueryFacedeI;
import org.lanqiao.ssm.facede.auth.model.User;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;


@SuppressWarnings("all")
@RestController
@RequestMapping("/user")
@Api(value = "user", description = "有关于用户的CURD操作", position = 5)
public class UserController extends BaseController<User, String> {
	@Resource
	private UserManagerFacedeI userManagerFacede;
	@Resource
	private UserQueryFacedeI userQueryFacede;

	@Override
	public BaseManagerFacedeI<User, String> getManagerFacede() {
		return userManagerFacede;
	}

	@Override
	public BaseQueryFacedeI<User, String> getQueryFacede() {
		return userQueryFacede;
	}

	@ApiOperation(value = "显示用户", httpMethod = "GET", notes = "显示用户")
	@RequestMapping(value = "/{id}/showUser", method = RequestMethod.GET)
	public String showUser(@PathVariable String id, HttpServletRequest request) {
		User u = userQueryFacede.selectByPrimaryKey(id);
		request.setAttribute("user", u);
		return "showUser";
	}

	@RequestMapping(value = "/findUserAndRolesByPage", method = RequestMethod.GET)
	public EasyuiModel<User> findUserAndRolesByPage(User user, int page, int rows) throws Exception {
		PageInfo<User> usersPage = userQueryFacede.findUserAndRoles(user, page, rows);
		return ModelConvertUtil.pageInfo2EasyuiModel(usersPage);
	}

	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	public Json edit(User user) throws Exception {
		return userManagerFacede.edit(user) > 0 ? setSimpleSuccess(user) : setFailed();
	}

}
